﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using AudioPlayer.Entity;
namespace AudioPlayer.GUI.Music {
	/// <summary>
	/// Group for music nodes.
	/// </summary>
	internal class MusicGroup  {
		#region Fields
		private String _text;

		private MusicNodeCollection _nodes;

		private Rectangle _bounds;
		
		private Boolean _expandable;
		private Boolean _expanded;
		#endregion
		#region Constructor
		public MusicGroup(string text) {
			this.Text = text;

			this.Nodes = new MusicNodeCollection(this);
		}
		#endregion
		#region Private Voids
		private void SetEmptyBounds() {
			if (_nodes != null && _nodes.Count > 0) {
				for (int i = 0; i < _nodes.Count; i++) {
					_nodes[i].Bounds = Rectangle.Empty;
				}
			}
		}
		#endregion
		#region Protected
		/// <summary>
		/// Called when state has been changed.
		/// </summary>
		protected void OnStateChanged() {
			if (StateChanged != null) {
				this.StateChanged(this, EventArgs.Empty);
			}
		}
		#endregion
		#region Public Voids
		public void Toggle() {
			if (this.Expanded) {
				this.Expanded = false;
			} else {
				this.Expanded = true;
			}
		}
		#endregion
		#region Property
		public String Text {
			get { return _text; }
			set { _text=value; }
		}
		public MusicNodeCollection Nodes {
			get { return _nodes; }
			set { _nodes=value; }
		}
		public Rectangle Bounds {
			get { return _bounds; }
			set { _bounds=value; }
		}

		public int GroupHeight {
			get {
				int height = this.Bounds.Height;
				if (this.Nodes != null && Nodes.Count > 0) {
					for (int i = 0; i < this.Nodes.Count; i++) {
						height += this.Nodes[i].Bounds.Height;
					}
				}
				return height;
			}
		}

		public bool Expandable {
			get {
				return _expandable;
			}
			set {
				_expandable = value;
			}
		}

		public bool Expanded {
			get {
				return _expanded;
			}
			set {
				if (_expanded != value) {
					_expanded = value;
					if (_expandable) {
						this.SetEmptyBounds();
					}
					this.OnStateChanged();
				}
			}
		}
		#endregion
		#region Events
		/// <summary>
		/// Occurs when user click's group, and it's state has change.
		/// </summary>
		public event EventHandler StateChanged;
		#endregion
	}
}
